package com.group.yztcedu.playlive.main.home.adapter;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.group.yztcedu.playlive.bean.HomeChannelsBean;
import com.group.yztcedu.playlive.main.home.WebViewActivity;
import com.group.yztcedu.playlive.main.playvideo.PlayVideoActivity;

import java.util.List;


public class ImagePagerAdapter extends PagerAdapter {
    private List<HomeChannelsBean> mList;

    public ImagePagerAdapter(List<HomeChannelsBean> list) {
        this.mList = list;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        ImageView imageView = new ImageView(container.getContext());
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        String picUrl = mList.get(position % mList.size()).getPicUrl();
        Glide.with(container.getContext()).load(picUrl).into(imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position % mList.size() == 0) {
                    container.getContext().startActivity(new Intent(container.getContext(), WebViewActivity.class));
                } else {
                    int roomId = mList.get(position % mList.size()).getUser().getRoomId();
                    Intent intent = new Intent(container.getContext(),PlayVideoActivity.class);
                    intent.putExtra("roomId",roomId+"");
                    container.getContext().startActivity(intent);

                }

            }
        });
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((ImageView) object);
    }
}
